
package com.zdkj.subway.rca.service.impl;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.zdkj.subway.rca.dto.SwCkElectronicReceiptConfirmDTO;
import com.zdkj.subway.rca.entity.*;
import com.zdkj.subway.rca.enums.SW_CK_ELECTRONIC_RECEIPT_RETURN_ORDER_TYPE;
import com.zdkj.subway.rca.service.SwCkElectronicReceiptService;

import com.zdkj.subway.rca.service.ViewCkMarketingTicketElecService;
import com.zdkj.subway.rca.service.base.SwCkElectronicReceiptServiceBase;
import com.zdkj.subway.rca.util.LocalDateConverter;
import com.zdkj.subway.rca.util.LocalDateTimeConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.*;

/**
 * 电子账单
 *
 * @author cooze
 * @date 2020-10-24 21:52:04
 */
@Slf4j
@Service
public class SwCkElectronicReceiptServiceImpl extends SwCkElectronicReceiptServiceBase implements SwCkElectronicReceiptService {


    @Autowired
    private ViewCkMarketingTicketElecService viewCkMarketingTicketElecService;


    @Override
    public List<CodeName> getAllTicketType() {
        return baseMapper.getAllTicketType();
    }

    @Override
    public IPage<SwCkElectronicReceipt> page(IPage<SwCkElectronicReceipt> page, Wrapper<SwCkElectronicReceipt> queryWrapper) {
        IPage<SwCkElectronicReceipt> result = this.baseMapper.selectPage(page, queryWrapper);
        if (CollectionUtils.isEmpty(result.getRecords())) {
            return result;
        }
        result.getRecords().forEach(super::setLineNameAndStationName);
        return result;
    }

    @Override
    public void exportExcel(SwCkElectronicReceiptExport export, HttpServletResponse response) {
        if (SW_CK_ELECTRONIC_RECEIPT_RETURN_ORDER_TYPE.isInValid(export.getReturnOrderType())) {
            log.error("ReturnOrderType not exist.");
            return;
        }
        //获取模板excel文件流
        try (InputStream excelTemplate = new PathMatchingResourcePatternResolver().getResource(Objects.requireNonNull(EXPORT_EXCEL_TPL.toEnum(export.getReturnOrderType())).getTplClasspath()).getInputStream()) {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-disposition", exportFileName(export));
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerConverter(new LocalDateTimeConverter()).registerConverter(new LocalDateConverter()).withTemplate(excelTemplate).build();
            //填充数据
            List<SwCkElectronicReceipt> results = queryExportDataResult(export);
            excelWriter.fill(results, EasyExcel.writerSheet(excelSheetName(export)).build());
            excelWriter.finish();
        } catch (Exception e) {
            log.error("SwCkElectronicReceiptServiceImpl export error.", e);
        }
    }

    @Override
    public void confirmStatus(SwCkElectronicReceiptConfirmDTO dto) {
        baseMapper.batchConfirmStatus(dto);
    }

    @Override
    public List<ViewCkMarketingTicketElec> queryCkMarketingTicketElec(String ticketType, String confirmStatus, String startDate, String endDate) {
        ViewCkMarketingTicketElec elec = new ViewCkMarketingTicketElec();
        boolean isDateCondition = StringUtils.isNotEmpty(startDate) && StringUtils.isNotEmpty(endDate);
        if (StringUtils.isNotEmpty(ticketType)) {
            elec.setTicketType(ticketType);
        }
        if (StringUtils.isNotEmpty(confirmStatus)) {
            elec.setConfirmStatus(confirmStatus);
        }
        List<ViewCkMarketingTicketElec> list = viewCkMarketingTicketElecService.list(Wrappers.query(elec).lambda()
                .between(isDateCondition, ViewCkMarketingTicketElec::getTradeTime, startDate, endDate));
        aggregationCkMarketingTicketElec(list);
        return list;
    }

}
